function charger_valeurs()
{
	var xmlHttp = createXmlHttpRequestObject();
	xmlHttp.open("GET", "lister_adherent.php", true);
	xmlHttp.onreadystatechange = function(){traiter_reponse(xmlHttp);};
	xmlHttp.send(null);
}

function traiter_reponse(myReq) 
{
	var ol;
	var li;
	var responseJSON;
	
	if (myReq.readyState == 4) 
	{
		
		if(myReq.status == 200) 
		{
			// On remplit la liste des adh�rents
			responseJSON = JSON.parse(myReq.responseText);
			for(i = 0; i < responseJSON.listeAdherents.length; i++) 
			{     
				ol = document.getElementById('contenu_adherent');
				li = document.createElement("OPTION"); 
				li_text = document.createTextNode(responseJSON.listeAdherents[i].nom+" "+responseJSON.listeAdherents[i].prenom);
				li.appendChild(li_text);     
				ol.appendChild(li); 
			} 
			
			// On remplit la liste des parcelles
			for(i = 0; i < responseJSON.listeParcelles.length; i++) 
			{     
				ol = document.getElementById('contenu_parcelle');
				li = document.createElement("OPTION"); 
				li_text = document.createTextNode("Parcelle : "+responseJSON.listeParcelles[i].id_parcelle);
				li.appendChild(li_text);     
				ol.appendChild(li); 
			} 
			
			// On remplit la liste des parcelles
			for(i = 0; i < responseJSON.listeParcellesAdherents.length; i++) 
			{     
				ol = document.getElementById('contenu_adherent_parcelle');
				li = document.createElement("OPTION"); 
				li_text = document.createTextNode(responseJSON.listeParcellesAdherents[i].nom+" "+responseJSON.listeParcellesAdherents[i].prenom+"  Parcelle : "+responseJSON.listeParcellesAdherents[i].id_parcelle);
				li.appendChild(li_text);     
				ol.appendChild(li); 
			} 		
		}	
	} 
}

function ajouterParcelleAdherent()
{
	var indexAdherent = document.getElementById('contenu_adherent');
	var adherentValue = indexAdherent.options[indexAdherent.selectedIndex].value;
	
	var indexParcelle = document.getElementById('contenu_parcelle');
	var parcelleValue = indexParcelle.options[indexParcelle.selectedIndex].value;
	
	if(adherentValue != null && parcelleValue != null)
	{
		var xmlHttp = createXmlHttpRequestObject();
		var tab = adherentValue.split(' ');
		var nom = tab[0];
		var prenom = tab[1];
		tab = parcelleValue.split(' ');
		var idParcelle = tab[2];
		
		var params = "nom="+nom+"&prenom="+prenom+"&id="+idParcelle;
		xmlHttp.open("POST", "ajouter_adherentParcelle.php", true);
		xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		xmlHttp.setRequestHeader("Content-length", params.length);
		xmlHttp.setRequestHeader("Connection", "close");
		xmlHttp.send(params);
		xmlHttp.onreadystatechange = function() 
		{
			if (xmlHttp.readyState == 4) 
			{		
				if(xmlHttp.status == 200) 
				{
					location.reload();
				}
			}
		}		
	}	
}

function supprimerParcelleAdherent()
{
	var indexLocation = document.getElementById('contenu_adherent_parcelle');
	var locationValue = indexLocation.options[indexLocation.selectedIndex].value;
	
	if(locationValue != null)
	{
		var xmlHttp = createXmlHttpRequestObject();
		var tab = locationValue.split(' ');
		var idParcelle = tab[4];
		
		var params = "id="+idParcelle;
		xmlHttp.open("POST", "supprimer_adherentParcelle.php", true);
		xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		xmlHttp.setRequestHeader("Content-length", params.length);
		xmlHttp.setRequestHeader("Connection", "close");
		xmlHttp.send(params);
		xmlHttp.onreadystatechange = function() 
		{
			if (xmlHttp.readyState == 4) 
			{		
				if(xmlHttp.status == 200) 
				{
					location.reload();
				}
			}
		}		
	}	
}

function ajouterEvent()
{
	addListener(document.getElementById('annuler'),'click',function(){history.back()});
	addListener(document.getElementById('bt_ajouter'),'click',ajouterParcelleAdherent);
	addListener(document.getElementById('bt_supprimer'),'click',supprimerParcelleAdherent);
}

function stopEvent(e) {
	if(e.stopPropagation){
		e.stopPropagation();
		e.preventDefault();
	}
 
	if(window.event){
		window.event.cancelBubble = true;
		window.event.returnValue  = false;
		return;
	}
}

function checkForm(event)
{
	faulty=null;
	if(!faulty)
	{
		return stopEvent(event);
	}
 }


function addFormCheck(nomForm){
var form = document.getElementById('form1');
var form2 = document.getElementById('form2');
addListener(form,'submit',checkForm);
addListener(form2,'submit',checkForm);
}

addListener(window,'load',addFormCheck);
addListener(window,'load',ajouterEvent);
addListener(window,'load',charger_valeurs);